/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int val){this.val = val;}
 *     ListNode(int val,ListNode next){this.val = val; this.next = next;}
 * }
 **/

public class Solution {
    public ListNode reverseList(ListNode head){
        if (head == null){
            return head;
        }
        ListNode l1 = null;
        ListNode l2 = head;
        while (l2 != null){
            ListNode tmp = l2.next;
            l2.next = l1;
            l1 = l2;
            l2 = tmp;
        }
        return l1;
    }
}
